import axios from "axios";

import qs from 'qs';
import {get_token, remove_token, set_token} from '@/utils/token'
import {Message} from "element-ui";
import router from "@/router"
import {refresh_token} from "@/api/auth"
import switchEnv from "@/utils/global"
//接口域名
axios.defaults.baseURL = switchEnv().BASE_API;

//get方法：
const get = (url, params) => {
    return new Promise((resolve, reject) => {
        axios.get(url, {params: params})
            .then((response) => {
                resolve(response.data);
            })
            .catch((error) => {
                reject(error);
            });
    })
};

//post方法：
const post = (url, data) => {
    return new Promise((resolve, reject) => {
        axios.post(url, qs.stringify(data), {
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        })
            .then((response) => {
                resolve(response.data);
            })
            .catch((error) => {
                reject(error);

            });
    })
};

//post方法：
const post_json = (url, data) => {
    return new Promise((resolve, reject) => {
        axios.post(url, data, {})
            .then((response) => {
                resolve(response.data);
            })
            .catch((error) => {
                reject(error);
            });
    })
};


// delete请求
const del = (url, data) => {
    return new Promise((resolve, reject) => {
        axios.delete(url, {data})
            .then((response) => {
                resolve(response.data);
            })
            .catch((error) => {
                reject(error);
            });
    })
};


// put请求
const put = (url, data) => {
    return new Promise((resolve, reject) => {
        axios.put(url, qs.stringify(data), {
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        })
            .then((response) => {
                resolve(response.data);
            })
            .catch((error) => {
                reject(error);

            });
    })
};


// put请求
const put_json = (url, data) => {
    return new Promise((resolve, reject) => {
        axios.put(url, data, {})
            .then((response) => {
                resolve(response.data);
            })
            .catch((error) => {
                reject(error);

            });
    })
};

// 请求拦截器
axios.interceptors.request.use(function (config) {
    let token = get_token();
    if (token) {
        config["headers"].token = token;
    }
    return config;
}, err => {
    return Promise.reject(err)
});


// 响应拦截器
axios.interceptors.response.use(function (res) {

    //返回401 去登录
    if (res.data.status === '401') {
        Message.error(res.data.msg);
        router.push({path: '/index'});
        remove_token();
        return
    }

    //返回402刷新token
    if (res.data.status === 402) {
        let token = get_token();
        return refresh_token(token.refresh_token).then(resp => {
            set_token(resp.data);
            return axios(res.config)
        }).catch(() => {
            router.push({path: '/index'});
            remove_token();
            return;
        })
    }

    //其他类型异常
    if (res.data.status !== 200) {
        Message.error(res.data.msg);
        return
    }
    return res;
}, err => {
    //服务器出错
    return Promise.reject(err);
});

export {
    post,
    get,
    del,
    put,
    post_json,
    put_json
}
